﻿using System.Windows;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System;

namespace Silverlight.InputCtrl
{
    public static class ValidatorManager
    {
        public static void ResetAllValidators()
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> items = from v in OC.elementValidators where v.Enabled select v;
            foreach (var item in items)
            {
                item.Reset();
                ValidatorCommon.OnReset(item.ElementValidator);
            }
        }
        public static void ResetValidators()
        {
            ResetValidators("",true);
        }
        public static void ResetValidators(string GroupValidate)
        {
            ResetValidators(GroupValidate, true);
        }
        public static void ResetValidators(string GroupValidate, bool casesentitive)
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> itemgroup;
            if (!casesentitive)
            {
                itemgroup = from v in OC.elementValidators where v.Group.ToLower() == GroupValidate.ToLower() && v.Enabled select v;
            }
            else
            {
                itemgroup = from v in OC.elementValidators where v.Group == GroupValidate && v.Enabled select v;
            }
            foreach (var item in itemgroup)
            {
                item.Reset();
                ValidatorCommon.OnReset(item.ElementValidator);

            }
        }

        public static string[] GetAllErrors()
        {
            ValidateAll();
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> items = from v in OC.elementValidators where v.Enabled select v;
            List<string> errs = new List<string>();
            foreach (var item in items)
            {
                if (!item.IsValidResult)
                {
                    errs.Add(item.ErrorMessage);
                }
            }
            return errs.ToArray();
        }
        public static bool ValidateAll()
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> items = from v in OC.elementValidators where v.Enabled select v;
            bool ret = true;
            foreach (var item in items)
            {
                ValidatorCommon.OnValidate(item.ElementValidator);
                if (!item.IsValidResult)
                {
                    ret = false;
                }
            }
            return ret;
        }

        public static string[] GetErrors()
        {
            return GetErrors("", true);
        }
        public static string[] GetErrors(string GroupValidate)
        {
            return GetErrors(GroupValidate, true);
        }
        public static string[] GetErrors(string GroupValidate, bool casesentitive)
        {
            Validate(GroupValidate, casesentitive);
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> itemgroup;
            if (!casesentitive)
            {
                itemgroup = from v in OC.elementValidators where v.Group.ToLower() == GroupValidate.ToLower() && v.Enabled select v;
            }
            else
            {
                itemgroup = from v in OC.elementValidators where v.Group == GroupValidate && v.Enabled select v;
            }
            List<string> errs = new List<string>();
            foreach (var item in itemgroup)
            {
                if (!item.IsValidResult)
                {
                    errs.Add(item.ErrorMessage);
                }
            }
            return errs.ToArray();
        }
        public static bool Validate()
        {
            return Validate("", true);
        }
        public static bool Validate(string GroupValidate)
        {
            return Validate(GroupValidate, true);
        }

        public static bool Validate(string GroupValidate,bool casesentitive)
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> itemgroup;
            if (!casesentitive)
            {
                itemgroup = from v in OC.elementValidators where v.Group.ToLower() == GroupValidate.ToLower() && v.Enabled select v;
            }
            else
            {
                itemgroup = from v in OC.elementValidators where v.Group == GroupValidate && v.Enabled select v;
            }

            bool ret = true;
            foreach (var item in itemgroup)
            {
                ValidatorCommon.OnValidate(item.ElementValidator);
                if (!item.IsValidResult)
                {
                    ret = false;
                }
            }
            return ret;
        }

        public static void EnabledGroup(string GroupValidate)
        {
            EnabledGroup(GroupValidate, true);
        }

        private static void EnabledGroup(string GroupValidate,bool casesentitive)
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> itemgroup;
            if (!casesentitive)
            {
                itemgroup = from v in OC.elementValidators where v.Group.ToLower() == GroupValidate.ToLower() && v.Enabled == false select v;
            }
            else
            {
                itemgroup = from v in OC.elementValidators where v.Group == GroupValidate && v.Enabled == false select v;
            }
            foreach (var item in itemgroup)
            {
                item.Enabled = true;
            }
        }

        public static void DisabledGroup(string GroupValidate)
        {
            DisabledGroup(GroupValidate, true);
        }

        private static void DisabledGroup(string GroupValidate, bool casesentitive)
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            IEnumerable<IBaseValidator> itemgroup;
            if (!casesentitive)
            {
                itemgroup = from v in OC.elementValidators where v.Group.ToLower() == GroupValidate.ToLower() && v.Enabled select v;
            }
            else
            {
                itemgroup = from v in OC.elementValidators where v.Group == GroupValidate && v.Enabled select v;
            }
            foreach (var item in itemgroup)
            {
                item.Enabled = false;
                FrameworkElement fe = item.ElementValidator;
                if (fe != null)
                {
                    var elemMsgs = ValidatorManager.FindElementsErrorMessage(fe.Name);
                    foreach (var itemMsg in elemMsgs)
                    {
                        itemMsg.ElementMessage.Visibility = Visibility.Collapsed;                        
                    }
                    Behaviortrigger bt = fe.GetValue(ValidatorService.behaviorProperty) as Behaviortrigger;
                    ValidatorCollection cbv = fe.GetValue(ValidatorService.ValidatorsProperty) as ValidatorCollection;
                    if (bt.BorderIndicator)
                    {
                        ((Control)fe).BorderBrush = cbv.ElementBorder;
                        ((Control)fe).BorderThickness = cbv.ElementBorderThickness;
                    }
                    if (bt.MessageTooltip)
                    {
                        ToolTipService.SetToolTip(fe, cbv.ElementTooltip);
                    }   
                }
            }
        }

        public static void RegisterValidator(IBaseValidator item)
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            OC.elementValidators.Add(item);
        }

        public static void RegisterElementMessage(FrameworkElement item, ValidatorErrorMessage VldErrorMessage)
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                throw new Exception(Resource.ValidatorManager_ContextNotFound);
            }
            if (!Application.Current.Resources.Contains("__ValidatorManager.ErroMessage"))
            {
                Application.Current.Resources.Add("__ValidatorManager.ErroMessage", new ObservableCollection<ErroMessageElementInfo>());
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            OC.elementMessages.Add(new ErroMessageElementInfo() { ElementMessage = item, ErrorMessage = VldErrorMessage});
        }

        public static void InitContext(FrameworkElement Context,bool Reset)
        {
            if (Reset)
            {
                if (Application.Current.Resources.Contains("__ValidatorManager.Context"))
                {
                    Application.Current.Resources.Remove("__ValidatorManager.Context");
                }
                Application.Current.Resources.Add("__ValidatorManager.Context", new ContextValidator(Context));
            }
            else
            {
                if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
                {
                    throw new Exception(Resource.ValidatorManager_ContextNotFound);
                }
                ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
                OC.elementMessages = new List<ErroMessageElementInfo>();
                OC.elementValidators = new List<IBaseValidator>();
                Application.Current.Resources["__ValidatorManager.Context"] = OC;
            }
        }

        public static void UnRegisterValidator(IBaseValidator item)
        {
            if (Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
                string Name = item.ElementValidator.Name;
                OC.elementValidators.Remove(item);
                UnRegisterElementMessage(Name);
            }
        }

        public static void UnRegisterElementMessage(string ElementValidate)
        {
            if (Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
                IEnumerable<ErroMessageElementInfo> info = from i in OC.elementMessages where i.ErrorMessage.ElementValidator == ElementValidate select i;
                foreach (var item in info)
                {
                    OC.elementMessages.Remove(item);
                }
            }
        }

        internal static ErroMessageElementInfo[] FindElementsErrorMessage(string ElementName)
        {
            List<ErroMessageElementInfo> msgs = new List<ErroMessageElementInfo>();

            if (Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
                IEnumerable<ErroMessageElementInfo> items;
                items = from m in OC.elementMessages where m.ErrorMessage.ElementValidator == ElementName select m;
                foreach (var item in items)
                {
                    msgs.Add(item);
                }
            }
            return msgs.ToArray();
        }

        public static FrameworkElement GetContextValidator()
        {
            if (!Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                return null;
            }
            ContextValidator OC = Application.Current.Resources["__ValidatorManager.Context"] as ContextValidator;
            return OC.ContextElement;
        }

    }
}
